<template>
	<uni-popup ref="popup" type="center" :safe-area="false" :is-mask-click="false" @maskClick="_tapMask">
		<view class="pop-center">
			<view class="pop-body">
				<view class="pop-title">{{title}}</view>
				<view class="pop-content">
					<view class="row" style="justify-content: space-between;">
						<view class="desc">{{desc1}}</view>
					</view>
					<view class="row">
						<view class="url" @click="tapAction('url')">{{urlTitle}}</view>
					</view>
					<view class="row">
						<view class="desc">{{desc2}}</view>
					</view>
					<view class="row">
						<button class="clear-button op-button op-normal" @click="handleDisagree">不同意</button>
						<button id='agree-btn' class="clear-button op-button op-confirm"
							open-type="agreePrivacyAuthorization"
							@agreeprivacyauthorization="handleAgree">同意并继续</button>
					</view>
				</view>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	let privacyHandler;
	let privacyResolves = new Set();
	let closeOtherPagePopUpHooks = new Set()
	//#ifdef MP-WEIXIN
	wx.onNeedPrivacyAuthorization((resolve, eventInfo) => {
		if (typeof privacyHandler === 'function') {
			privacyHandler(resolve);
		}
	})
	//#endif
	const closeOtherPagePopUp = (closePopUp) => {
		closeOtherPagePopUpHooks.forEach(hook => {
			if (closePopUp !== hook) {
				hook()
			}
		})
	}
	import PopComponentMixin from "../../mixins/common-pop-mixin.js";
	export default {
		mixins: [PopComponentMixin],
		name: "wx-privacy-pop",
		data: function() {
			return {
				title: "用户隐私保护提示",
				desc1: "感谢您使用本应用，您使用本应用前应当阅读并同意",
				urlTitle: "《用户隐私保护指引》",
				desc2: "当您点击同意并开始时用产品服务时，即表示你已理解并同息该条款内容，该条款将对您产生法律约束力。如您拒绝，部分功能将无法正常使用。"
			};
		},
		methods: {
			// commonmixin reload
			_triggerPageShow() {
				const closePopUp = () => {
					this.showPop(false);
				}
				privacyHandler = resolve => {
					privacyResolves.add(resolve)
					this.showPop(true)
					// 额外逻辑：当前页面的隐私弹窗弹起的时候，关掉其他页面的隐私弹窗
					closeOtherPagePopUp(closePopUp)
				}

				closeOtherPagePopUpHooks.add(closePopUp)
				this.closePopUp = closePopUp
			},
			_triggerPageHide() {
				closeOtherPagePopUpHooks.delete(this.closePopUp)
			},
			tapAction(type) {
				switch (type) {
					case 'url':
						this.openPrivacyContract()
						break
					default:
						break
				}
			},
			_tapMask() {

			},
			handleAgree(e) {
				this.showPop(false);
				// 让隐私弹窗保持单例，点击一次同意按钮即可让所有pending中的wx隐私接口继续执行
				privacyResolves.forEach(resolve => {
					resolve({
						event: 'agree',
						buttonId: 'agree-btn'
					})
				})
				privacyResolves.clear()
			},
			handleDisagree(e) {
				this.showPop(false)
				privacyResolves.forEach(resolve => {
					resolve({
						event: 'disagree',
					})
				})
				privacyResolves.clear()
			},
			openPrivacyContract() {
				// #ifdef MP-WEIXIN
				wx.openPrivacyContract({
					success: res => {
						console.log('openPrivacyContract success')
					},
					fail: res => {
						console.error('openPrivacyContract fail', res)
					}
				})
				// #endif
			}
		}
	}
</script>

<style lang="scss" scoped>
	.desc {
		color: #888;
	}

	.url {
		color: #3AA6D0;
		text-decoration: underline;
	}
</style>